package uy.edu.um.ltic.server.communication;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import uy.edu.um.ltic.commons.EmployeeRemoteMgt;
import uy.edu.um.ltic.commons.EmployeeVO;
import uy.edu.um.ltic.exception.DataBaseException;
import uy.edu.um.ltic.server.business.*;


public class EmployeeRemoteServerMgr implements EmployeeRemoteMgt{

	private static EmployeeRemoteServerMgr instance = null;

	private EmployeeRemoteServerMgr() throws RemoteException{
				
	}

	public static EmployeeRemoteServerMgr getInstance()  throws RemoteException{
		if (instance == null) {
			instance = new EmployeeRemoteServerMgr();
		}
		return instance;
	}


	@Override
	public void addEmployee(EmployeeVO dtEmployee)  throws 
	RemoteException, DataBaseException {
		EmployeeFactory oFac = EmployeeFactory.getInstance();
		EmployeeMgt oEMgt = oFac.getEmployeeMgt();
		oEMgt.addEmployee(dtEmployee);
		
	}

	@Override
	public void deleteEmployee(int onEmployee)  throws 
	RemoteException, DataBaseException {
		EmployeeFactory.getInstance().getEmployeeMgt().deleteEmployee(onEmployee);
		
	}

	@Override
	public void editEmployee(EmployeeVO oEmployeeVO)  throws 
	RemoteException, DataBaseException {
		EmployeeFactory.getInstance().getEmployeeMgt().editEmployee(oEmployeeVO);
		
	}

	@Override
	public List<EmployeeVO> getEmployees(EmployeeVO oEmployeeVO)  throws 
	RemoteException, DataBaseException {
		EmployeeFactory oFac = EmployeeFactory.getInstance();
		EmployeeMgt oEMgt = oFac.getEmployeeMgt();
		ArrayList<Employee> oLista = oEMgt.getEmployees(oEmployeeVO);
		ArrayList<EmployeeVO> oListaOV = new ArrayList<EmployeeVO>(); 
		for (Employee s : oLista){
		    oListaOV.add(s.convertToEmployeeVO());
		}
	return oListaOV;
	}

	@Override
	public EmployeeVO getEmployeeLogIn(String username)
			throws RemoteException, DataBaseException {
			EmployeeVO oEmployeeVOR = null;
			Employee oEmployee = EmployeeFactory.getInstance().getEmployeeMgt().getEmployeeLogIn(username);
			if (oEmployee!=null){
				oEmployeeVOR = oEmployee.convertToEmployeeVO();
			}
			
			return oEmployeeVOR;
	}

	
	public void deleteEmployee(EmployeeVO oEmployeeVO)throws RemoteException, DataBaseException {
		EmployeeFactory.getInstance().getEmployeeMgt().deleteEmployee(oEmployeeVO.getnEmployee());
	}

	@Override
	public void setStatus(String status, int nEmployee) throws RemoteException, DataBaseException {
		EmployeeFactory.getInstance().getEmployeeMgt().setStatus(status, nEmployee);
		
	}

	@Override
	public EmployeeVO getEmployeeNEmp(int nEmployee) throws RemoteException, DataBaseException {
		return EmployeeFactory.getInstance().getEmployeeMgt().getEmployeeNEmp(nEmployee).convertToEmployeeVO();
	}

	@Override
	public ArrayList<EmployeeVO> getAllEmployees() throws RemoteException, DataBaseException {
		ArrayList<Employee> oLEmp = EmployeeFactory.getInstance().getEmployeeMgt().getAllEmployees();
		ArrayList<EmployeeVO> oLEmpVO = new ArrayList<EmployeeVO>();
		for(Employee emp : oLEmp){
			oLEmpVO.add(emp.convertToEmployeeVO());
		}
		return oLEmpVO;
	}
}
